package com.xnyzc.lhy.mis.controller.cmsapi.coupon;


import com.xnyzc.lhy.common.entity.CouponRv;
import com.xnyzc.lhy.common.exception.CouponException;
import com.xnyzc.lhy.common.penum.coupon.CouponErrorCode;
import com.xnyzc.lhy.mis.entity.coupon.*;
import com.xnyzc.lhy.mis.service.coupon.IOaSysCouponBatchService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 优惠券批次 前端控制器
 * </p>
 *
 * @author haoyfa
 * @since 2019-12-16
 */
@RestController
@RequestMapping("/coupon/couponBatch")
@Slf4j
public class OaSysCouponBatchController {
    @Autowired
    IOaSysCouponBatchService iOaSysCouponBatchService;

    /**
     * 获取优惠券批次列表(根据优惠券定义ID)
     *
     * @return
     */
    @ApiOperation(value = "获取优惠券批次列表(根据优惠券定义ID)", notes = "获取优惠券批次列表(根据优惠券定义ID)")
    @GetMapping(value = "/queryCouponBatchList")
    public CouponRv queryCouponBatchList(@RequestParam("defId") String defId) {
        try {
            List<CouponBatchSearchResult> couponBatchSearchResultList = iOaSysCouponBatchService.queryCouponBatchList(defId);
            return CouponRv.wrapSuccess(couponBatchSearchResultList);
        }
        catch (CouponException ae){
            log.error("获取优惠券批次列表(根据优惠券定义ID)失败：" + ae.getErrno().getDesc());
            return CouponRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("获取优惠券批次列表(根据优惠券定义ID)失败：" + CouponErrorCode.unKnow.getDesc());
            return CouponRv.wrapErrorCode(CouponErrorCode.unKnow);
        }
    }

    /**
     * 新增优惠券批次
     *
     * @return
     */
    @ApiOperation(value = "新增优惠券批次", notes = "新增优惠券批次")
    @PostMapping(value = "/createCouponBatch")
    public CouponRv createCouponBatch(@RequestBody CouponBatchParam couponBatchParam) {
        try {
            iOaSysCouponBatchService.createCouponBatch(couponBatchParam);
            return CouponRv.wrapSuccess();
        }
        catch (CouponException ae){
            log.error("新增优惠券批次 失败：" + ae.getErrno().getDesc());
            return CouponRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("新增优惠券批次 失败：" + CouponErrorCode.unKnow.getDesc());
            return CouponRv.wrapErrorCode(CouponErrorCode.unKnow);
        }
    }

    /**
     * 有效优惠券查询
     *
     * @return
     */
    @ApiOperation(value = "有效优惠券查询", notes = "有效优惠券查询")
    @GetMapping(value = "/queryEffectiveCouponList")
    public CouponRv queryEffectiveCouponList() {
        try {
            List<CouponEffectiveResult> couponEffectiveResultList = iOaSysCouponBatchService.queryEffectiveCouponList();
            if(couponEffectiveResultList != null){
                return CouponRv.wrapSuccess(couponEffectiveResultList);
            }else{
                return CouponRv.wrapSuccess(new Object());
            }
        }
        catch (CouponException ae){
            log.error("有效优惠券查询 失败：" + ae.getErrno().getDesc());
            return CouponRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("有效优惠券查询 失败：" + CouponErrorCode.unKnow.getDesc());
            return CouponRv.wrapErrorCode(CouponErrorCode.unKnow);
        }
    }

    /**
     * 发放优惠券
     *
     * @return
     */
    @ApiOperation(value = "发放优惠券", notes = "发放优惠券")
    @PostMapping(value = "/publishCoupon")
    public CouponRv publishCoupon(@RequestBody CouponPublishParam couponBindParam) {
        try {
            iOaSysCouponBatchService.publishCoupon(null, couponBindParam);
            return CouponRv.wrapSuccess(new Object());
        }
        catch (CouponException ae){
            log.error("绑定优惠券 失败：" + ae.getErrno().getDesc());
            return CouponRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("绑定优惠券 失败：" + CouponErrorCode.unKnow.getDesc());
            return CouponRv.wrapErrorCode(CouponErrorCode.unKnow);
        }
    }

    /**
     * 批量发放优惠券
     *
     * @return
     */
    @ApiOperation(value = "批量发放优惠券", notes = "批量发放优惠券")
    @PostMapping(value = "/batchPublishCoupon")
    public CouponRv batchPublishCoupon(@RequestBody CouponBatchPublishParam couponBatchPublishParam) {
        try {
            iOaSysCouponBatchService.batchPublishCoupon(couponBatchPublishParam);
            return CouponRv.wrapSuccess(new Object());
        }
        catch (CouponException ae){
            log.error("绑定优惠券 失败：" + ae.getErrno().getDesc());
            return CouponRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("绑定优惠券 失败：" + CouponErrorCode.unKnow.getDesc());
            return CouponRv.wrapErrorCode(CouponErrorCode.unKnow);
        }
    }
}
